/*
 * Copyright (c) 2017,安徽皖通科技股份有限公司 All rights reserved.
 */

package com.wttech.dzzf.process.dao;

import com.wtkj.dao.impl.ListResultSetExtractor;
import com.wtkj.vo.dzzf.base.kfwd.CardInfo;
import com.wtkj.vo.dzzf.base.kfwd.FundAccount;
import com.wtkj.vo.dzzf.base.kfwd.VehicleInfo;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * 建行数据检测DAO实现
 * @author MaShibo
 * @date 2017/12/5.
 */
@Repository
public class CbcBankDataCheckDao {

	@Resource(name = "jdbcTemplate")
	private JdbcTemplate jdbcTemplate;


	public int queryFundAccountNum(){
		final String sql = "SELECT COUNT(1) FROM AC_FUNDACCOUNT";
		return jdbcTemplate.queryForObject(sql, Integer.class);
	}

	@SuppressWarnings("unchecked")
	public List<FundAccount> queryPageFundAccountInfos(int startRow, int endRow){
		final String sql =
				"SELECT *\n" +
						"  FROM (SELECT A.*, J.CHANNELID, ROWNUM AS RM\n" +
						"          FROM AC_FUNDACCOUNT A\n" +
						"          LEFT JOIN JG_ORGANIZATION J\n" +
						"            ON A.CLIENTNO = J.CODE)\n" +
						" WHERE RM >= ?\n" +
						"   AND RM <= ?";
		return (List<FundAccount>)jdbcTemplate.query(sql, new ListResultSetExtractor(new FundAccount()), startRow, endRow);
	}

	@SuppressWarnings("unchecked")
	public List<CardInfo> queryCardInfoByFundAccountId(String fundAccountId){
		final String sql =
				"SELECT C.* " +
						"  FROM AC_CARDINFO C " +
						"  JOIN AC_ACCOUNT T " +
						"    ON C.ACCOUNTID = T.ACCOUNTID " +
						"  JOIN AC_FUNDACCOUNT F " +
						"    ON T.FUNDACCOUNTID = F.FUNDACCOUNTID" +
						" WHERE F.FUNDACCOUNTID = ?";
		return (List<CardInfo>) jdbcTemplate.query(sql, new ListResultSetExtractor(new CardInfo()), fundAccountId);
	}

	@SuppressWarnings("unchecked")
	public VehicleInfo queryVehInfosByVeh(String vehPlate, Integer vehColor){
		final String sql = "SELECT * FROM AC_VEHICLEINFO A WHERE A.VEHPLATE=? AND A.VEHCOLOR=? ";
		List<VehicleInfo> vehicleInfos = (List<VehicleInfo>) jdbcTemplate.query(sql, new ListResultSetExtractor(new VehicleInfo()),vehPlate, vehColor);
		return CollectionUtils.isEmpty(vehicleInfos) ? null : vehicleInfos.get(0);
	}

}
